package com.devshephard.jucks.threadSafeCollections.blockingQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class blockQueueTest {

    public static void main(String[] args) throws InterruptedException {
        //test1.test();
        //test2.test();
       // test3.test();
        test4.test();
    }

}


class test1{

    public static void test(){
        //基本的阻塞队列操作，add 添加 remove 移除
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        arrayBlockingQueue.add(1);
        arrayBlockingQueue.add(2);
        arrayBlockingQueue.add(3);
        //arrayBlockingQueue.add(4); //当超过已限定大小的阻塞队列时，会报错

        arrayBlockingQueue.remove();
        arrayBlockingQueue.remove();
        arrayBlockingQueue.remove();
        //arrayBlockingQueue.remove(); //当移除已限定大小的阻塞队列时，会报错
    }

}

class test2{

    public static void test(){
        //不会报错的基本的阻塞队列操作，offer 添加 poll 移除
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(arrayBlockingQueue.offer(1));
        System.out.println(arrayBlockingQueue.offer(2));
        System.out.println(arrayBlockingQueue.offer(3));
        System.out.println(arrayBlockingQueue.offer(4));  //当添加已限定大小的阻塞队列时，不会报错，返回false

        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());   //当获取已限定大小的阻塞队列时，不会报错，返回null

    }

}


class test3{

    public static void test() throws InterruptedException {
        //不会报错的基本的阻塞队列操作，put 添加 take 移除
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        arrayBlockingQueue.put(1);
        arrayBlockingQueue.put(2);
        arrayBlockingQueue.put(3);
        //arrayBlockingQueue.put(4);  //当添加已限定大小的阻塞队列时，不会报错，但会一直阻塞

        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());   //当获取已限定大小的阻塞队列时，不会报错，但会一直阻塞

    }

}


class test4{

    public static void test() throws InterruptedException {
        //不会报错的基本的阻塞队列操作，offer 添加 poll 移除
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(arrayBlockingQueue.offer(1));
        System.out.println(arrayBlockingQueue.offer(2));
        System.out.println(arrayBlockingQueue.offer(3));
        new Thread(()->{
            arrayBlockingQueue.poll(); //测试将其中一个元素移除，看看是否能够将超时等待中的值存入队列中
        }).start();
        //超时等待，如果等待后依旧没有位置存入，则结束阻塞从而退出
        System.out.println(arrayBlockingQueue.offer(4,4, TimeUnit.SECONDS));

        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        //超时等待，如果等待后依旧没有位置取出，则结束阻塞从而退出
        System.out.println(arrayBlockingQueue.poll(2, TimeUnit.SECONDS));

    }

}